package com.caiya.service;

import com.caiya.cart.Res;
import com.caiya.cart.entity.CartUser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import rx.Observable;

/**
 * Created by Administrator on 2019/4/8 0008.
 */
@Service
public class AggregationService {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<Res> getUserById(Long id){
        return Observable.create(observer ->{
            Res cartUser = restTemplate.getForObject("http://caiya-cart/api/user/byid/{id}",Res.class,id);
            observer.onNext(cartUser);
            observer.onCompleted();
        });
    }

    public Observable<Res> getMovieUserByUserId(Long id){
        return Observable.create(observer ->{
            Res movieUser = restTemplate.getForObject("http://caiya-cart-movie/api/user/byid/{id}",Res.class,id);
            observer.onNext(movieUser);
            observer.onCompleted();
        });
    }

    public Res fallback(Long id){
        Res cartUser = new Res(0,"错误");
        return cartUser;
    }
}
